package com.smartrobot.smartrobot.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.circle.FriendList;
import com.smartrobot.smartrobot.circle.commonData;
import com.smartrobot.smartrobot.pojo.circleDetailPojo;
import com.smartrobot.smartrobot.view.ToastCustom;

/**
 * Created by yeluojun on 2015/1/19.
 */
public class Soc_detail_activity extends Activity implements View.OnClickListener{

    private TextView circleMaster,circleNumber;
    private ProgressBar detail_loading;
    private RelativeLayout detail_content;
    private ImageView back;
    public static boolean isGettingCircleDetail = false;


    private FriendList friendList;
    private  circleDetailPojo c_pojo;
    private Handler callback_handler;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            throw new NullPointerException();
        }
        this.setContentView(R.layout.page_social_circle_circle_detail);
        initView();

    }

    private void initView(){
        circleMaster  = (TextView)findViewById(R.id.circleMaster);
        circleNumber  = (TextView)findViewById(R.id.userCount);
        detail_content = (RelativeLayout)findViewById(R.id.detail_content);
        detail_loading = (ProgressBar)findViewById(R.id.circle_detail_loading);
        back = (ImageView)findViewById(R.id.circle_detail_back);
        detail_content.setVisibility(View.GONE);
        callback_handler = new circle_detail_callback_handler();
        context = getApplicationContext();

        back.setOnClickListener(this);

        getCircleDetail();
    }



    public void getCircleDetail(){
        if(!isGettingCircleDetail){
//            progressBar.setVisibility(View.VISIBLE);
            friendList  = new FriendList();
            friendList.getCircleDetail(callback_handler,context);
//            isGettingCircleDetail = true;
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.circle_detail_back:

                Soc_detail_activity.this.finish();

                break;

        }

    }


    class circle_detail_callback_handler extends Handler {

        @Override
        public void dispatchMessage(Message msg){

            //链接服务器失败
            if(msg.what == commonData.CAN_NOT_CONNECT_SERVER_CODE){
                detail_loading.setVisibility(View.GONE);
                ToastCustom.showToast(context, msg.obj.toString());

                isGettingCircleDetail = false;
                return;
            }
            //获取圈子详情成功
            if(msg.what == commonData.GET_CIRCLE_DETAIL_SUCCESS_CODE){
                detail_content.setVisibility(View.VISIBLE);
                detail_loading.setVisibility(View.GONE);
                c_pojo = new circleDetailPojo();
                c_pojo = (circleDetailPojo)msg.obj;
                isGettingCircleDetail = false;
                setCircleDetail();
                return;
            }

            //获取圈子详情失败
            if(msg.what == commonData.GET_CIRCLE_DETAIL_FAIL_CODE){
                detail_loading.setVisibility(View.GONE);
                isGettingCircleDetail = false;
                return;
            }

            //需要显示的tip
            if(msg.what==commonData.SHOW_TIP_CODE){
                detail_loading.setVisibility(View.GONE);
                isGettingCircleDetail = false;
                ToastCustom.showToast(context,msg.obj.toString());
                return;
            }
        }
    }


    public void setCircleDetail(){

        String showName = c_pojo.getCircleMasterPhone();
        String nickName = c_pojo.getCircleMasterNickName();
        String reName = c_pojo.getCircleMasterName();

        if(nickName!=null&&!nickName.equals("")){
            showName = nickName;
        }else if(reName!=null&&!reName.equals("")){
            showName = reName;
        }
      circleMaster.setText(showName);
      circleNumber.setText(c_pojo.getUserCount());
//        progressBar.setVisibility(View.GONE);
    }
}
